//import { loginByUsername, logout, getUserInfo } from '@/service/_api/user'
import { getToken, setToken, removeToken  , getQueryString } from '@/utils/auth';
//import storage from '@/libs/storage/session.js'
import { thirdLogin } from '@/service/swagger/ThirdController';
import { logout } from '@/service/swagger/UserController';
import { getMenuByUserId } from '@/service/swagger/MenuController';
import session from '@/libs/storage/session';
import router from '@/router/index.js';


const user = {
	state: {
		userMsg : {},
		//  user: '',
		//  status: '',
		//  code: '',
		//  token: getToken(),
		//  name: '',
		//  avatar: '',
		//  introduction: '',
		//  roles: [],
		//  setting: {
		//    articlePlatform: []
		//  }
	},

	mutations: {
		//  SET_CODE: (state, code) => {
		//    state.code = code
		//  },
		//  SET_TOKEN: (state, token) => {
		//    state.token = token
		//  },
		//  SET_INTRODUCTION: (state, introduction) => {
		//    state.introduction = introduction
		//  },
		//  SET_SETTING: (state, setting) => {
		//    state.setting = setting
		//  },
		//  SET_STATUS: (state, status) => {
		//    state.status = status
		//  },
		//  SET_NAME: (state, name) => {
		//    state.name = name
		//  },
		//  SET_AVATAR: (state, avatar) => {
		//    state.avatar = avatar
		//  },
		//  SET_ROLES: (state, roles) => {
		//    state.roles = roles
		//  }
		SET_USERMSG:(state,msg)=>{
			state.userMsg = msg;
		},
		
	},

	actions: {
		setUserInfo : ({ commit }) => {
			return new Promise((resolve, reject) => {
				const code = getQueryString("code");
				const oldCode = session.get("code")
				const msg = session.get("userInfo")
				// console.log(msg,222222222222222222)
				if((msg&&code==oldCode)||process.env.NODE_ENV == 'development'){
					commit('SET_USERMSG', JSON.parse(msg))
					resolve(msg)
				}else{
					thirdLogin({
						code : code,
						type : 2,
					}).then(ret => {
		        if (!ret.code) {
							console.log(ret.data,222222222222222222)
		           const data = ret.data
		           session.set("userInfo", JSON.stringify(data))
		           session.set("code", code)
							 commit('SET_USERMSG', data)
							 router.go(0)
		           resolve(msg)
		        }else{
		         		resolve(false)
		         		window.location.href = window.loginUrl ;
		        }
		      })
				}
			})
   	},
		LogOut: () => {
			let vm = this ;
			return new Promise((resolve, reject) => {
					const token = (this.a.state.user||this.a.state).userMsg.token ;
					logout(token).then(ret=>{
						session.clear();
						resolve()
         		window.location.href = window.loginUrl ;
					})
			})
		}
	}
}

export default user